var PersonalCenter={
	// 设置账户密码	
	AddAccountPwd:function(){
	    var password=$.trim($("#password").val());
	    if(password==""){
	    	YM.page.alert("请输入您的密码！");
	        return;
	    }
	    
	    if(password.length != 6){
	    	YM.page.alert("请输入6位密码,数字或字母,区分大小写！");
	        return;
	    }
	    
	    $.ajax({
	        type: "POST",
	        url: "/personalCenter/addAccountPwd.jspa",
	        dataType:'json',
	        data:"passWord="+ password,
	        success: function(json){
	            if(json.flag == '1'){
	            	YM.page.alert("支付密码设置成功！");
	                $("#accountPwd-D").remove();
	            }else if(json.flag=='0'){
	            	YM.page.alert("请重新登录！");
	            }else if(json.flag=="-1"){
	            	YM.page.alert(json.msg);
	            }else{
	            	YM.page.alert("支付密码设置失败，请刷新后重试！");
	            }
	        }
	     }); 
	},

	// 取消关注
	CancelAttention:function(goodsIds){
		YM.page.confirm('您确认要取消关注该商品吗?', function() {
    		YM.ajax("/personalCenter/cancelAttention.jspa", {goodsIds:goodsIds}, function(d) {
    			YM.page.alert(d, function() {
					YM.page.refresh();
				});
            }, 'String');
    	});
	},
	
	// 分仓库存切换
	showAreaStock:function(houseId,goodsIds,_text){
		$.ajax({
			url : '/personalCenter/showAreaStock.jspa',
			cache : false,
			data : "goodsIds="+goodsIds+"&houseId="+houseId,
			dataType : 'json',
			success : function(json) {
				var goodsId = goodsIds.split(",");
				var id="";
				for(var i=0; i<goodsId.length; i++){
					id=goodsId[i];
					if(json[id]==0) {
						$("#collect_"+id).html("无货");
						$("#btn_"+id).html("");
					} else if(json[id]==1) {
						$("#collect_"+id).html("有货");
						$("#btn_"+id).html(_text.replaceAll("{goodsId}", id));
					} else if(json[id]==2) {
						$("#collect_"+id).html("预订");
						$("#btn_"+id).html(_text.replaceAll("{goodsId}", id));
					}
				}
			}
		});
	},
	
	// Tab切换
	setTab:function(name,cursel,n,fn){
	     for(var i=1;i<=n;i++){
	      var tab=document.getElementById(name+i);
	      var con=document.getElementById("con_"+name+"_"+i);
	      tab.className=i==cursel?"on":"";
	      con.style.display=i==cursel?"block":"none";
	      if(i==cursel){
	    	   if(fn){
	    		   fn();
	    	   }
	      }
	   }
	},
	
	// 个人资料切换
	ChangeTab:function(index){
		if(index=="1"){
			window.location.href = "/personalCenter/showUserInfo.jspa";
		}else if(index=="2"){
			window.location.href = "/personalCenter/showMyHonor.jspa";
		}else if(index=="3"){
			window.location.href = "/personalCenter/showMyAddress.jspa";
		}
	},
	
	// 会籍显示
	MemberClass:function(){
		YM.dataproxy.ajax({
			url : YM.login.servicePath +'personalCenter/searchMemberClass.jspa',
			cache : false,
			dataType : 'json',
			success : function(res) {
				if(res!=null && res.flag!=null && res.flag==0){
					YM.page.alert(res.msg);
					return;
				}else{
					
					var effectiveScore = res.effectiveScore==null?0:res.effectiveScore;
					var gradeScore = res.gradeScore==null?0:res.gradeScore;
					
					$("#hjmemberClass").html(res.memberLevelName);
					$("#hjnextMemberClass").html(res.memberClass!=res.nextLevelClass? res.nextLevelName : "你目前会员的级别已是最高");
					$("#hjupdateCondition").html(res.memberClass!=res.nextLevelClass? (effectiveScore>=res.upgradeScore ? "恭喜您！已经达到升级积分" : '新增 <b class="orange">'+(res.upgradeScore-effectiveScore)+'</b> 积分') : "你目前会员的级别已是最高");
					$("#hjendDate").html(res.memberClass!="MEMBER_LEVEL_NORMAL" ? res.endDate : "无限期");
					$("#hjkeepLevelContidtion").html(res.memberClass!="MEMBER_LEVEL_NORMAL" ? '新增 <b class="green">'+res.demoteScore+'</b> 积分/年' : "无条件");
					
					var nowScore = gradeScore==0 ? effectiveScore:gradeScore;
					$("#hjkeepLevelScore").html(res.memberClass!="MEMBER_LEVEL_NORMAL" ? (nowScore>=res.demoteScore ? '恭喜您！已经达到保级积分':'还需新增 <b class="green">'+(res.demoteScore-nowScore)+'</b> 积分') : "不需要积分");

					
					PersonalCenter.initMemberClassHistory(res.historyList);
				}
			}
		});
	},
	
	// 会籍变化记录
	initMemberClassHistory:function(historyList){
		var html='';
		for(var i=0;i<historyList.length;i++){
			html+='<li><span>'+historyList[i].changeDate+'&nbsp;</span>';
			
			var info='';
			info = historyList[i].toLevel!='MEMBER_LEVEL_NORMAL'? historyList[i].toLevelShortName + 'VIP' : '普通会员';
			if(historyList[i].isUpgrade==0){//降级
				info='降级为'+info;
			}else if(historyList[i].isUpgrade==1){//升级
				info='升级为'+info;
			}else{//延长
				info+='延长有效期';
			}
			html+=info+'</li>';
		}
		$("#hjmemberClassHistroyList").html(html);
	},
	
	// 去修改用户地址
	toUpdateAddress:function(addressId){
		$.ajax({
			   type: "POST",
			   url: "/neworder/getAddress.jspa",
			   data : {addressId:addressId},
			   dataType : 'json',
			   async : false,
			   success:function(json){
			   		if(json.infoDTO.flag == 1){
			   			PersonalCenter.initAddressInfo(json);
			   		}else{
			   			YM.page.alert(json.infoDTO.msg);
			   		}
			   }
		});
	},
	
	// 填充用户地址信息
	initAddressInfo:function(json){
		$("#addressId").val(json.memberAddress.addressId);
		$("#name").val(json.memberAddress.name);
		$("#address").val(json.memberAddress.address);
		$("#zipCode").val(json.memberAddress.zipCode);
		var phone = json.memberAddress.phone;
		
		if(phone!=null && json.memberAddress.phone!=""){
			var phoneArr = phone.split("-");
			if(phoneArr.length==1){
				$("#phone2").val(phoneArr[0]);
			}else{
				$("#phone1").val(phoneArr[0]);
				$("#phone2").val(phoneArr[1]);
			}
		}
		$("#mobile").val(json.memberAddress.mobile);
		
		// 修改省市
		var areaId = json.memberAddress.areaId;
		if(areaId!=null && areaId!=""){
			$.ajax({
				   type: "POST",
				   url: "/personalCenter/findAddressByAreaId.jspa",
				   data : {third:areaId},
				   dataType : 'json',
				   async : false,
				   success:function(json){
				   		if(json != null && json!="-1" && json!="0"){
				   			area.curFirst = json.A1ID;
				   			area.curSecond = json.A2ID;
				   			area.curThird = json.A3ID;
				   			area.init();
				   		}
				   }
			});
		}
	},
	
	// 删除收货地址 
	deleteAddress:function(addressId){
		YM.page.confirm('您确认要删除该收货地址吗?', function() {
			if(addressId==null || addressId==""){
				YM.page.alert("收货地址不能为空!");
			}
			
			YM.ajax("/personalCenter/deleteUserAddressAjax.jspa", {addressId:addressId}, function(msg) {
    			if (msg=='success') {
    				YM.page.alert("地址删除成功！", function() {
    					YM.page.refresh();
    				});
                }else{
                	YM.page.alert(msg);
                }
            }, 'String');
    	});
		
		
	},

	// 取消订单
	cancelOrder:function(orderId){
		YM.page.confirm('您确认要取消该订单吗?', function() {
			location.href="/personalCenter/cancelOrderInfo.jspa?orderId="+orderId;	
    	});
	},
	
	// 加入购物车
	addCartGoods:function(goodsId, num){
		YM.page.cart.addGoodsToCartV2({goodsId:goodsId,goodsNum:num});
		
		//(goodsId, num, null, null, function(){
		//	YM.page.header.cart.update();
		//});
	}
};